package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.stairactivity.WriteArticleActivity;
import com.wangtao.firstapp.fragments.homefragments.CommunityFragment;
import com.wangtao.firstapp.fragments.homefragments.HomePagerFragment;
import com.wangtao.firstapp.fragments.homefragments.MessageFragment;
import com.wangtao.firstapp.fragments.homefragments.PersonFragment;

/**
 * 主页面
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager manager;//创建Fragment管理者
    private FragmentTransaction transaction;//创建事物

    private RadioButton rb_main, rb_community, rb_publish, rb_msg, rb_personal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initEvent();
    }

    private void initView() {

        rb_main = (RadioButton) findViewById(R.id.rb_main);
        rb_community = (RadioButton) findViewById(R.id.rb_community);
        rb_publish = (RadioButton) findViewById(R.id.rb_publish);
        rb_msg = (RadioButton) findViewById(R.id.rb_msg);
        rb_personal = (RadioButton) findViewById(R.id.rb_personal);
    }

    private void initData() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.linearLayout_act_main, new HomePagerFragment());
        transaction.commit();
    }

    private void initEvent() {
        rb_main.setOnClickListener(this);
        rb_community.setOnClickListener(this);
        rb_publish.setOnClickListener(this);
        rb_msg.setOnClickListener(this);
        rb_personal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
        switch (v.getId()) {
            case R.id.rb_main:
                transaction.replace(R.id.linearLayout_act_main, new HomePagerFragment());
                break;
            case R.id.rb_community:
                transaction.replace(R.id.linearLayout_act_main, new CommunityFragment());
                break;
            case R.id.rb_publish:
                Intent intent=new Intent(MainActivity.this,WriteArticleActivity.class);
                startActivity(intent);
                break;
            case R.id.rb_msg:
                transaction.replace(R.id.linearLayout_act_main, new MessageFragment());
                break;
            case R.id.rb_personal:
                transaction.replace(R.id.linearLayout_act_main, new PersonFragment());
                break;
        }
        transaction.commit();
    }
}
